﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Web.UI;
namespace TaiLieu
{
    public class FileManagement
    {
        public static FileInfo[] GetAllFileImg(string pathDirectory)
        {

            FileInfo[] files = new FileInfo[0];
            try
            {
                DirectoryInfo dir = new DirectoryInfo(pathDirectory);
                if (dir.Exists == true)
                {
                    files = dir.GetFiles();
                }
            }
            catch { }
            return files;
        }
        // lấy 1 hình
        public static string GetPathImg(string pathDirectory)
        {
            string tam = "";
            if (GetAllFileImg(pathDirectory).Length > 0)
            {

                tam = GetAllFileImg(pathDirectory)[0].Name;

            }
            return tam;
        }
        // kiểm tra file videos .flv
        public static string GetFileVideos(string pathDirectory)
        {
            string tam = "";
            foreach (FileInfo info in GetAllFileImg(pathDirectory))
            {
                if (info.FullName.ToLower().Contains(".flv") == true)
                { tam += info.Name + ";"; }
            }
            return tam;
        }
        // lấy nhiều hình 
        public static string GetPathImgs(string pathDirectory)
        {
            string tam = "";
            if (GetAllFileImg(pathDirectory).Length > 0)
            {
                foreach (FileInfo info in GetAllFileImg(pathDirectory))
                {
                    tam += info.FullName + ";";
                }
            }
            return tam;
        }
        public static DirectoryInfo CreatedFolder(string path)
        {
            DirectoryInfo di = new DirectoryInfo(path);
            if (!di.Exists)
            {
                return Directory.CreateDirectory(path);
            }
            return null;
        }
        public static void CreatedFile(string path)
        {
            FileInfo di = new FileInfo(path);
            if (!di.Exists)
            {
                //return File.Create(path);
            }
            // return null;
        }

        public static string GetFileName(string filePath)
        {
            if (filePath == "")
                return "";
            else
            {
                try
                {
                    int index = filePath.LastIndexOf("\\");
                    if (index == -1)
                    {
                        index = filePath.LastIndexOf("/");
                    }
                    return filePath.Substring(index + 1, filePath.Length - index - 1);
                }
                catch { }
            }
            return "";
        }

        static void writeFile(string path, string text)
        {
            FileInfo file = new FileInfo(@"d:\data.txt");
            StreamReader read = file.OpenText();
            string content = read.ReadToEnd();
            read.Close();
            StreamWriter writer = file.CreateText();
            writer.WriteLine(content.Replace("}}", text + "}}"));
            writer.Close();
        }
        public static string readFile()
        {
            FileInfo file = new FileInfo(@"D:\tai lieu\data\hoa hoc\12\Hoa\Este.doc");
            StreamReader read = file.OpenText();
            string content1 = read.ReadToEnd();
            string content = "";
            int lenght = read.ReadLine().Length;
            for (int index = 0; index <= lenght; index++)
            {
                string tam = read.ReadLine();
                if ((tam.Contains("A.") == true || tam.Contains("B.") == true || tam.Contains("C.") == true || tam.Contains("D.")) && tam.EndsWith("true") == true)
                {
                    content += "<a style=\"text-decoration:underline\">" + tam + "</a> ";
                }
                else
                    content += tam;
            }
            read.Close();
            return content;

        }
    }
}
